<script>
import { reactive, toRefs } from "vue";
import axios from "axios";
import { setSessionStorage } from "@/common";
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus';

export default {
    setup() {
        //实例化
        const router = useRouter()
        //定义需要绑定的数据
        const state = reactive({
            users: {
                userId: "",
                password: ""
            }
        })

        //当用户点击登录按钮时调用login函数
        function login() {
            //算法怎么写？1.非空校验 2.递交请求给服务端（结果有两种情况：成功或失败）
            //   console.log(state.users.userId)
            //   console.log(state.users.password)

            if (state.users.userId == '') {
                ElMessage({
                    type: 'warning',
                    message: '手机号不能为空！',
                })
                return;
            }

            if (state.users.password == '') {
                ElMessage({
                    type: 'warning',
                    message: '密码不能为空！',
                })
                return;
            }

            //都不为空
            axios.post('/api/users/login3', state.users).then((response) => {     //  response = [ApiRestResponse]
                console.log(response.data)
                let apires = response.data // apires  -- ApiRestResponse
                //获取服务端返回的用户信息
                let user = apires.data
                console.log("服务端返回的用户信息：" + user.userId + ":" + user.password)
                // if(u==''){
                //     alert('手机号或者密码错误！')
                //     return;
                // }else{
                //     //跳转到首页面
                //     router.push('/index')
                // }
                if (apires.status == 200) {
                    //先保存用户信息到会话域   sessionStorage
                    // sessionStorage.setItem("user",user)
                    ElMessage({
                        type: 'success',
                        message: '登录成功！',
                    })
                    setSessionStorage("user", user)
                    router.push('/index')
                } else {
                    ElMessage({
                        type: 'error',
                        message: apires.desc,
                    })
                    return;
                }
            }).catch((error) => {
                console.log(error)
            })
        }

        //跳转到注册页面
        function toRegister() {
            router.push('/register')
        }

        function toUpdatePass() {
            router.push('/updatepass')
        }

        return { //把数据和函数暴露出去
            ...toRefs(state),
            login,
            toRegister,
            toUpdatePass
        }
    }
}
</script>



<template>
    <!-- 总容器 -->
    <div class="wrapper">
        <h1>体检预约-登录</h1>
        <section>
            <div class="input-box">
                <i class="fa fa-user-o"></i>
                <input type="text" placeholder="输入手机号" v-model="users.userId">
            </div>
            <div class="input-box">
                <i class="fa fa-lock"></i>
                <input type="password" placeholder="输入登录密码" v-model="users.password">
            </div>
            <div class="reg-box">
                <p @click="toRegister">注册</p>
                <p @click="toUpdatePass">忘记密码？</p>
            </div>
            <!-- <el-button class="button-box" :plain="true" @click="login">登录</el-button> -->
            <div class="button-box" @click="login">登录</div>
        </section>

        <footer>
            <div>
                <div class="line"></div>
                <p>有疑问请联系客服</p>
                <div class="line"></div>
            </div>
            <p>4008-XXX-XXX</p>
        </footer>
    </div>
</template>



<style scoped>
/*********************** 总容器 ***********************/
.wrapper {
    width: 100%;
    height: 100%;
    background-image: linear-gradient(320deg, #8497d7, #5ca6b4, #8497d7);
    overflow: hidden;
    padding-bottom: 200px;
}

/*********************** 标题部分 ***********************/
h1 {
    text-align: center;
    color: #FFF;
    font-size: 9.5vw;
    /* v适口 w宽 h高 */
    font-weight: 500;
    margin-top: 13vh;
    margin-bottom: 3vh;
}

/*********************** 内容部分 ***********************/
section {
    width: 86vw;
    margin: 0 auto;
    background-color: #FFF;
    border-radius: 2.4vw;

    box-sizing: border-box;
    padding: 6vw;
}

section .input-box {
    width: 100%;
    height: 12vw;
    border: solid 1px #CCC;
    border-radius: 2vw;
    margin-top: 5vw;

    display: flex;
    align-items: center;
}

section .input-box i {
    margin: 0 3.6vw;
    font-size: 5.4vw;
    color: #CCC;
}

section .input-box input {
    border: none;
    outline: none;
}

section .reg-box {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-top: 5vw;
    margin-bottom: 2vw;
}

section .reg-box p {
    font-size: 3.3vw;
    color: #2D727E;
    user-select: none;
    cursor: pointer;
}

section .button-box {
    width: 100%;
    height: 13vw;
    border-radius: 2vw;
    background-color: #70B0BC;

    text-align: center;
    line-height: 13vw;
    font-size: 4.6vw;
    color: #FFF;
    letter-spacing: 1vw;

    user-select: none;
    cursor: pointer;
}

/*********************** footer部分 ***********************/
footer {
    width: 86vw;
    margin: 0 auto;
    margin-top: 12vw;
    font-size: 3.8vw;
    color: #FFF;
}

footer div {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

footer div .line {
    width: 22vw;
    height: 1px;
    background-color: #FFF;
}

footer>p {
    text-align: center;
    margin-top: 2vw;
}
</style>